﻿/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;
using FileSystemDatabase;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of CouponStore.
	/// </summary>
	public class CouponStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		private JsonDatabase db;
		public void PersistCoupons(List<Dictionary<string,object>> Coupons)
		{
			db.Write<List<Dictionary<string,object>>>(Coupons,_site.Name);
		}

		public void PersistCoupon(Dictionary<string,object> Coupon)
		{
			db.Write<Dictionary<string,object>>(Coupon,Coupon["coupons_id"].ToString());
		}
		
		public CouponStore(Site site,HtmlWeb client)
		{
			db = new JsonDatabase("sites/" + site.Name + "/Coupons");
			_site = site;
			_client = client;
		}
		
		private Dictionary<string,object> GetCoupon(string CouponId)
		{
			List<Dictionary<string,object>> entries = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=load_coupons&coupons_id=" + CouponId + "&module=coupons");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			List<Dictionary<string,object>> categories = new List<Dictionary<string,object>>();
			List<Dictionary<string,object>> products = new List<Dictionary<string,object>>();
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{
					Dictionary<string,object> Coupon = (Dictionary<string,object>)json[key];					
					
					foreach(string cle in Coupon.Keys)
					{
						switch(cle)
						{
							case "categories":								
								ArrayList recs = (ArrayList)Coupon[cle];
								for(int i = 0;i < recs.Count;i++)
								{
									Dictionary<string,object> category = (Dictionary<string,object>)recs[i];
									categories.Add(category);
								}								
								break;
							case "products":								
								ArrayList produits = (ArrayList)Coupon[cle];
								for(int i = 0;i < produits.Count;i++)
								{
									Dictionary<string,object> product = (Dictionary<string,object>)produits[i];
									products.Add(product);
								}								
								break;
						}
					}
					
					Coupon["products"] = products;
					Coupon["categories"] = categories;
					entries.Add(Coupon);
				}
			}
			
			return entries[0];
		}
		
		public List<Dictionary<string,object>> GetCoupons()
		{
			List<Dictionary<string,object>> Coupons = new List<Dictionary<string,object>>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_coupons&module=coupons");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			Dictionary<string,object> json = serialiser.Deserialize<Dictionary<string,object>>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					ArrayList recs = (ArrayList)json[key];
					for(int i = 0;i < recs.Count;i++)
					{
						Dictionary<string,object> record = (Dictionary<string,object>)recs[i];
						Dictionary<string,object> Coupon = GetCoupon(record["coupons_id"].ToString());
						Coupons.Add(Coupon);
					}
				}
			}
			
			return Coupons;
		}
	}
}
